package com.chen.base.module.comment;

import java.math.BigDecimal;

/**
 * @Title: manager-spring-mvc-hb
 * @Description: to do
 * @Author: chenjiwei
 * @Date: 2021-01-02 1:44
 * @Version: 1.0.0
 */
public enum OperatorConfig {
    ADD('+', 1),
    SUBTRACT('-', 1),
    MULTIPLY('*', 2),
    DIVIDE('/', 2),
    BRACKET_LEFT('(', 0),
    BRACKET_RIGHT(')', 0);

    // 运算符
    private char typeid;

    // 运算优先级
    private int priority;

    public char getTypeid(){
        return this.typeid;
    }
    public int getPriority(){
        return this.priority;
    }

    private OperatorConfig(char typeid, int priority){
        this.typeid = typeid;
        this.priority = priority;
    }


    public boolean isMaxAndEqueal(OperatorConfig operatorConfig){
        return this.priority >= operatorConfig.getPriority();
    }

    public static OperatorConfig getByTypeid(char typeid) throws Exception {
        for(OperatorConfig o : OperatorConfig.values()){
            if(o.getTypeid() == typeid)
                return o;
        }
        throw new Exception("非法运算符："+typeid);
    }

    public BigDecimal calculate(BigDecimal arg1, BigDecimal arg2) throws Exception {
        switch (this.typeid) {
            case '+' : return arg1.add(arg2);
            case '-' : return arg1.subtract(arg2);
            case '*' : return arg1.multiply(arg2);
            case '/' : return arg1.divide(arg2, 8, BigDecimal.ROUND_HALF_DOWN);
        }
        throw new Exception("非法运算符："+this.typeid);
    }


}
